/* make new directory */
local dir `c(pwd)'
capture mkdir length

/* cut the file into N (number depends on the number of cores on your machine)
groups, then open N instances of Stata to speed up processing */
use data_bible_final, clear

gen random=runiform()

	egen group = cut(random), group(`1') label
	
sum group
local min=r(min)
local max=r(max)

forvalues g=`min'/`max' {
	preserve
	keep if group==`g'
	save "`dir'\bible_index_`g'", replace
	/************ insert the working directory of Stata here ************/
	winexec C:\Program Files (x86)\Stata14\StataMP-64 -q do `dir'\length.do `g'
	restore
}

clear         
/* wait until everything is finished */
clear
 forvalues g=`min'/`max' {  
   capture confirm file finished_`g'.dta
   while _rc != 0 {
      sleep 2000
      capture confirm file finished_`g'.dta
   }
 }

forvalues g=`min'/`max' {
	capture erase finished_`g'.dta
}

exit
